<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
// 
//  search_model.php
//  phpweb
//  
//  Created by WillLee on 2011-07-10.
//  Copyright 2011 lishuzu. All rights reserved.
// 
class Search_model extends CI_Model
{
		function __construct(){
			parent::__construct();
			$this->load->model('user_model');
			$this->load->model('work_model');
			$this->load->model('tag_model');
			$this->user = $this->session->userdata('user') ? $this->session->userdata('user') : '';
			$this->uid = $this->uri->segment(3) ? intval($this->uri->segment(3)) : $this->user['uid'];
		}
		
		//默认搜索
		function get_all($searchkey){
			$data = $this->get_work($searchkey);
			$userdata = $this->get_user($searchkey, 3);
			if ($userdata['list']) {
				$data['userlist'] = $userdata['list'];
			}
			return $data;
		}

		// 搜索作品
		function get_work($searchkey, $limit=''){
			$list = $multipage_config  = array();	
			$searchtype = 'work';
			//URL参数
			$mpurl = base_url().'search/index/';
			$mpurl .= '?'.'searchkey='.$searchkey.'&searchsubmit=yes'.'&searchtype='.$searchtype;
			$offset = $this->input->get('per_page') ? intval($this->input->get('per_page', true)) : 0;
			//每页查看数
			$perpage = empty($_GET['perpage'])?0:intval($_GET['perpage']);
			if(!in_array($perpage, array(20,50,100))) $perpage = 20;
			
			//如果需要直接调用数量 
			if ($limit) {
				$perpage = intval($limit);
			}
			
			//分页参数
			$multipage_config = $this->config->item('multipage_config', 'SGLOBAL');
			$multipage_config['base_url'] = $mpurl;
			$multipage_config['page_query_string'] = TRUE;
			$multipage_config['uri_segment'] = '4';
			$multipage_config['per_page'] = $perpage;
			
			//查询数量
			$this->db->like('title',$searchkey);
			$this->db->where('status !=', 0);
			$this->db->where('status !=', 3);
			$count = $this->db->count_all_results("works");
			
			if($count) {
				$this->db->select('wid, uid, filepath, title, name, likenum, replynum, viewnum, tag');
				$this->db->like('title',$searchkey);
				$this->db->where('status !=', 0);
				$this->db->where('status !=', 3);
				$this->db->order_by('dateline', 'DESC');
				$query = $this->db->get("works", $multipage_config['per_page'], $offset);
				foreach ($query->result_array() as $row) {
					//处理关键字变红 加粗
					$row['searchtitle'] = str_replace($searchkey, $this->_keyfont($searchkey), $row['title']);
					$row['avatar_small'] = avatar($row['uid'], 'small');
					$row['thumbimg'] = get_workimg($row['filepath'], 'thumb', 'url');
					$row['url'] = base_url().'work/index/'.$row['wid'];
					$row['key'] = get_workkey($row['wid']);
					$row['tags'] = empty($row['tag'])?array():unserialize($row['tag']);
					$list[$row['wid']] = $row;
				}
				
				$multipage_config['per_page'] = $perpage;
				$multipage_config['total_rows'] = $count;
				$this->pagination->initialize($multipage_config);
				$multi = $this->pagination->create_links();
				$data['multi'] = $multi;
			}
			$data['list'] = $list;
			return $data;
		}
		
		
		// 搜索人
		function get_user($searchkey, $limit=''){
			$list = $multipage_config  = array();	
			$searchtype = 'user';
			//URL参数
			$mpurl = base_url().'search/index/';
			$mpurl .= '?'.'searchkey='.$searchkey.'&searchsubmit=yes'.'&searchtype='.$searchtype;
			$offset = $this->input->get('per_page') ? intval($this->input->get('per_page', true)) : 0;
			//每页查看数
			$perpage = empty($_GET['perpage'])?0:intval($_GET['perpage']);
			if(!in_array($perpage, array(20,50,100))) $perpage = 20;
			
			//如果需要直接调用数量 
			if ($limit) {
				$perpage = intval($limit);
			}
			
			//分页参数
			$multipage_config = $this->config->item('multipage_config', 'SGLOBAL');
			$multipage_config['base_url'] = $mpurl;
			$multipage_config['page_query_string'] = TRUE;
			$multipage_config['uri_segment'] = '4';
			$multipage_config['per_page'] = $perpage;
			
			//查询数量
			$this->db->like('name',$searchkey);
			$count = $this->db->count_all_results("user");
			// 获取数据
			if($count) {
				$this->db->select('uid, username, name, groupid, follow_count, fans_count, likenum, worknum, status, namestatus, passreason, ');
				$this->db->like('name',$searchkey);
				$query = $this->db->get("user", $multipage_config['per_page'], $offset);
				foreach ($query->result_array() as $row) {
					//处理关键字变红 加粗
					$row['searchname'] = str_replace($searchkey, $this->_keyfont($searchkey), $row['name']);
					$row['avatar_small'] = avatar($row['uid'], 'small');
					$list[$row['uid']] = $row;
				}
				
				$multipage_config['per_page'] = $perpage;
				$multipage_config['total_rows'] = $count;
				$this->pagination->initialize($multipage_config);
				$multi = $this->pagination->create_links();
				$data['multi'] = $multi;
			}
			$data['list'] = $list;
			return $data;
		}
		
		// 按照标签搜索作品
		function get_worktag($searchkey, $limit = ''){
			$list = $multipage_config  = array();	
			$searchtype = 'worktag';

			$mpurl = base_url().'search/index/';
			$mpurl .= '?'.'searchkey='.$searchkey.'&searchsubmit=yes'.'&searchtype='.$searchtype;
			$offset = $this->input->get('per_page') ? intval($this->input->get('per_page', true)) : 0;
			//每页查看数
			$perpage = empty($_GET['perpage'])?0:intval($_GET['perpage']);
			if(!in_array($perpage, array(20,50,100))) $perpage = 20;
			
			//如果需要直接调用数量 
			if ($limit) {
				$perpage = intval($limit);
			}
			
			//分页参数
			$multipage_config = $this->config->item('multipage_config', 'SGLOBAL');
			$multipage_config['base_url'] = $mpurl;
			$multipage_config['page_query_string'] = TRUE;
			$multipage_config['uri_segment'] = '4';
			$multipage_config['per_page'] = $perpage;
			
			//查询数量
			$wheresql = " w.status !=0 AND w.status !=3 AND t.tagname LIKE '%".$searchkey."%' ";
			$ordersql = " ORDER BY w.dateline DESC ";
			$groupbysql = " GROUP BY t.workid ";
			$result = $this->db->query("SELECT count(t.workid) as num FROM ".$this->db->dbprefix('works')." w LEFT JOIN ".$this->db->dbprefix('tagwork')." t ON t.workid=w.wid WHERE $wheresql $groupbysql")->row_array();
			$count = $this->db->affected_rows();
			
			// 获取数据		
			if($count) {
				$query = $this->db->query("SELECT w.wid, w.uid, w.filepath, w.title, w.name, w.likenum, w.replynum, w.viewnum, w.tag FROM ".$this->db->dbprefix('works')." w LEFT JOIN ".$this->db->dbprefix('tagwork')." t ON t.workid=w.wid WHERE $wheresql  $groupbysql $ordersql LIMIT $offset, ".$multipage_config['per_page']);
				foreach ($query->result_array() as $row) {
					//处理关键字变红 加粗
					$row['avatar_small'] = avatar($row['uid'], 'small');
					$row['thumbimg'] = get_workimg($row['filepath'], 'thumb', 'url');
					$row['url'] = base_url().'work/index/'.$row['wid'];
					$row['key'] = get_workkey($row['wid']);
					$row['tags'] = empty($row['tag'])?array():unserialize($row['tag']);
					if ($row['tags']) {
						foreach ($row['tags'] as $tagid=>$tagname) {
							$row['tags'][$tagid] = str_replace($searchkey, $this->_keyfont($searchkey), $tagname);
						}
					}
					$list[$row['wid']] = $row;
				}
				$multipage_config['total_rows'] = $count;
				$this->pagination->initialize($multipage_config);
				$multi = $this->pagination->create_links();
				$data['multi'] = $multi;
			}
			$data['list'] = $list;
			return $data;
		}
		
		
		
		// 按照标签ID搜索作品
		function get_tagid($tagid, $limit=''){
			$list = $multipage_config  = array();	
			$tagid = $tagid ? intval($tagid) : '';
			if (!$tagid) {
				return array();
			}
			$searchtype = 'tagid';
			$mpurl = base_url().'search/index/';
			$mpurl .= '?'.'searchkey='.$tagid.'&searchsubmit=yes'.'&searchtype='.$searchtype;
			$offset = $this->input->get('per_page') ? intval($this->input->get('per_page', true)) : 0;
			//每页查看数
			$perpage = empty($_GET['perpage'])?0:intval($_GET['perpage']);
			if(!in_array($perpage, array(20,50,100))) $perpage = 20;
			
			//如果需要直接调用数量 
			if ($limit) {
				$perpage = intval($limit);
			}
			
			//分页参数
			$multipage_config = $this->config->item('multipage_config', 'SGLOBAL');
			$multipage_config['base_url'] = $mpurl;
			$multipage_config['page_query_string'] = TRUE;
			$multipage_config['uri_segment'] = '4';
			$multipage_config['per_page'] = $perpage;
			
			//查询数量
			$wheresql = " w.status !=0 AND w.status !=3 AND t.tagid = '".$tagid."' ";
			$ordersql = " ORDER BY w.dateline DESC ";
			$result = $this->db->query("SELECT count(t.workid) as num FROM ".$this->db->dbprefix('works')." w LEFT JOIN ".$this->db->dbprefix('tagwork')." t ON t.workid=w.wid WHERE $wheresql")->row_array();

			$count = $this->db->affected_rows();

			// 获取数据		
			if($count) {
				$query = $this->db->query("SELECT w.wid, w.uid, w.filepath, w.title, w.name, w.likenum, w.replynum, w.viewnum, w.tag FROM ".$this->db->dbprefix('works')." w LEFT JOIN ".$this->db->dbprefix('tagwork')." t ON t.workid=w.wid WHERE $wheresql  $ordersql LIMIT $offset, ".$multipage_config['per_page']);
				foreach ($query->result_array() as $row) {
					//处理关键字变红 加粗
					$row['avatar_small'] = avatar($row['uid'], 'small');
					$row['thumbimg'] = get_workimg($row['filepath'], 'thumb', 'url');
					$row['url'] = base_url().'work/index/'.$row['wid'];
					$row['key'] = get_workkey($row['wid']);
					$row['tags'] = empty($row['tag'])?array():unserialize($row['tag']);
					if ($row['tags'][$tagid]) {
						$row['tags'][$tagid] = $this->_keyfont($row['tags'][$tagid]);
					}
					$list[$row['wid']] = $row;
				}
				$multipage_config['total_rows'] = $count;
				$this->pagination->initialize($multipage_config);
				$multi = $this->pagination->create_links();
				$data['multi'] = $multi;
			}
			$data['list'] = $list;
			return $data;
		}
		
		//让搜索词变红
		function _keyfont($string){
			return '<span class="red">'.$string.'</span>';
		}
}
?>